package com.yuda.tools.detection.controller;

import com.yuda.tools.detection.tool.joinCount;
import com.yuda.tools.detection.tool.subjectQuestionCount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
public class checkFormalAnswer {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @RequestMapping("/checkFormalAnswer")
    @ResponseBody
    public Map checkFormalAnswer() {
        Map map = new HashMap();
        String sub = "select subject_id from sparta_paper_subject_relation where jj_end_time < unix_timestamp(now())";
        System.out.println(sub);
        String answer1 = "select count(1) num from tsinghua_formal_answer where (answer='' or answer is null) and type not in ('5','6','7','8')";
        int except = jdbcTemplate.queryForObject(answer1, Integer.class);
        if (except > 0) {
            System.out.println("tsinghua_formal_answer表中答案有空的");
            map.put("tsinghua_formal_answer表中答案有空的:", except);
        }
        List<String> subject = jdbcTemplate.queryForList(sub, String.class);
        for (String subject_id : subject) {
            int student_num = new joinCount().joinCount(subject_id, jdbcTemplate);
            int ques_num = new subjectQuestionCount().count(subject_id, jdbcTemplate);
            String sql = "select count(1) from (select count(1) num from tsinghua_formal_answer where subject_id=" + subject_id + " group by student_id having count(1) =" + ques_num + ") aa";
            System.out.println(sql);
            int db_student_count = jdbcTemplate.queryForObject(sql, Integer.class);
            if (student_num == db_student_count) {
                System.out.println(subject_id + "参加考试的人数为：" + student_num + ",答案提交全的人数为：" + db_student_count + ",数量正确。");
//                sb.append(subject_id+"参加考试的人数为："+student_num+",答案提交全的人数为："+db_student_count+",数量正确。");
                String sql1 = "select zkz_num from sparta_exam_subject_student where student_id not in (select student_id from tsinghua_formal_answer where subject_id=" + subject_id + " group by student_id having count(1) =" + ques_num + ") and subject_id =" + subject_id + " and stu_start_time>'0' and student_id not in (select student_id from sparta_exam_student where examroom_id=647)";
                System.out.println(sql1);
                List<String> zkz_num = jdbcTemplate.queryForList(sql1, String.class);
                if (zkz_num.isEmpty()) {
                    map.put(subject_id, "检查结果正常");
                } else {
                    map.put(subject_id, zkz_num);
                }
                System.out.println(zkz_num);
//                sb.append(subject+""+zkz_num);
            } else {
                System.err.println(subject_id + "参加考试的人数为：" + student_num + ",答案提交全的人数为：" + db_student_count + ",数量错误。");
                //sb.append(subject_id+"参加考试的人数为："+student_num+",答案提交全的人数为："+db_student_count+",数量错误。");
                String sql1 = "select zkz_num from sparta_exam_subject_student where student_id not in (select student_id from tsinghua_formal_answer where subject_id=" + subject_id + " group by student_id having count(1) =" + ques_num + ") and subject_id =" + subject_id + " and stu_start_time>'0' and student_id not in (select student_id from sparta_exam_student where examroom_id=647)";
                System.out.println(sql1);
                List<String> zkz_num = jdbcTemplate.queryForList(sql1, String.class);
                System.out.println(zkz_num);
                if (!zkz_num.isEmpty()) {
                    map.put(subject_id, zkz_num);
                } else {
                    map.put(subject_id, "参加考试的人数为：" + student_num + ",答案提交全的人数为：" + db_student_count + ",数量错误。");
                }
            }

        }

        int c = 0;
        for (Object value : map.values()) {
            if (value instanceof String && value.equals("检查结果正常")) {
            }
            else {
                c++;
            }
        }

        if(c==0) {
            //g更新
            String sql2="update road_map set valid_answer=1";
            jdbcTemplate.update(sql2);
        }else{
            String sql2="update road_map set valid_answer=0";
            jdbcTemplate.update(sql2);
        }

        return map;
    }
}
